---
--类名: PriceInputView.lua
--创建日期: 2018-8-31
--创建人: 宋建邦
--功能描述:输入竞价视图
--修改内容:
--修改日期:
--修改人:
--修改备注:

local AuctionKeyboardPanel = require("app.auction.views.AuctionKeyboardPanel")

local PriceInputView = class("PriceInputView", app.mvc.ViewBase)
PriceInputView.VIEW_ROOT_RES = Res.PriceInputView

function PriceInputView:init()
    self:setBackGroundColorType(0)
    self:setNodeEventEnabled(true)

    self.title = self._root:findChild("Panel/Panel_tlt/tlt")
    self.title:setString(L("lua_code_text_362"))

    self.btnMinus = self._root:findChild("Panel/Panel_supplies/Button_minus")
    self.btnPlus = self._root:findChild("Panel/Panel_supplies/Button_plus")
    self.btnEdit = self._root:findChild("Panel/Panel_supplies/icon")

    self.panelPrice = self._root:findChild("Panel/Panel_supplies/num")
    self.panelPrice:setContentSize(cc.size(150, self.panelPrice:getContentSize().height))
    self.lbPrice = self._root:findChild("Panel/Panel_supplies/num/num")
    self.icnPrice = self._root:findChild("Panel/Panel_supplies/num/gold")
    self:setPrice(0)

    self.btnOK = self._root:findChild("Panel/Button_queding")
    self.btnCancel = self._root:findChild("Panel/Button_quxiao")

    self.keyBoard = self._root:findChild("Panel/Node_keyboard")

    self.curMoney = self._root:findChild("Panel/Panel_num_2/num")
    self.curMoneyIcon = self._root:findChild("Panel/Panel_num_2/gold")

    self.btnEdit:setTouchEnabled(true)
    self:initButtons()
    self:initMoney()
end

function PriceInputView:onExit()
end

function PriceInputView:refreshButton()
    self.btnMinus:setTouchEnabled(true)
    self.btnMinus:setBright(true)
    self.btnPlus:setTouchEnabled(true)
    self.btnPlus:setBright(true)

    if self.currentPrice <= 0 then
        self.btnMinus:setTouchEnabled(false)
        self.btnMinus:setBright(false)
    end

    if self.currentPrice >= self:getTotalMoney() then
        self.btnPlus:setTouchEnabled(false)
        self.btnPlus:setBright(false)
    end
end

function PriceInputView:initButtons()
    local step = 1
    
    self.btnMinus:addClickEventListener(function(sender)
        self:setPrice(math.max(self.currentPrice - step, 0) )
        self:refreshButton()
    end)

    self.btnPlus:addClickEventListener(function(sender) 
        self:setPrice(math.min(self.currentPrice + step, self:getTotalMoney()) )
        self:refreshButton()
    end)

    self.btnEdit:addClickEventListener(function(sender) 
            if not self.keyboardObj then
                self.keyboardObj = AuctionKeyboardPanel.create(self.keyBoard)
                self.keyboardObj:setCallFunc(function(a)                    
                    self:setPrice(checknumber(a))
                    self.keyboardObj:hide()
                    local EventDispatcher = cc.Director:getInstance():getEventDispatcher()
                    EventDispatcher:removeEventListener(self.kbListener)  
                end)
            end
            self.keyboardObj:setNumLimit(0,self:getTotalMoney())
            self.keyboardObj:setNum(self.currentPrice)
            self.keyboardObj:show()
            -- 点击空白区域关闭
            self.kbListener = display.nodeRemoveForNotHit(self.keyboardObj, function()
                if not cc.isDead( self.keyboardObj ) then
                    self.keyboardObj:performWithDelay(function()
                        -- self.keyboardObj:removeFromParent(true)
                        self.keyboardObj:hide()
                        -- self.keyboardObj = nil
                    end, 0)
                end
                local EventDispatcher = cc.Director:getInstance():getEventDispatcher()
                EventDispatcher:removeEventListener(self.kbListener)                
            end)
    end)

    self.btnOK:addClickEventListener(function(sender) 
        if self._data and self.currentPrice ~= checknumber(self._data.data.myBiddingPrice) then
            local c = app:getInst("BiddingController")
            c:reqPrice(self._data.data.id, self.currentPrice)
        end        

        UIHandler:removePopupUiByKey(self:getViewName())
    end)

    self.btnCancel:addClickEventListener(function(sender) 
        UIHandler:removePopupUiByKey(self:getViewName())
    end)
end

function PriceInputView:initMoney()
    local gm = app:getInst("NewguildModel")
    local ginfo = gm:getMyGuildInfo()
    local guildMoney = 0
    if ginfo then
        guildMoney = ginfo:getGuildFund()
    end
    self.guildMoney = guildMoney
    self.curMoney:setString(string.format(L("lua_code_text_1"), guildMoney))
    self:centerNodes({self.curMoney, self.curMoneyIcon})
    if self.guildMoney <= 0 then
        display.pushToast(L("tips_bidding_no_money"))
    end
end

function PriceInputView:refreshPrice(data)
    self.originalPrice = checknumber(data.data.myBiddingPrice)

    self:setPrice(self.originalPrice)
end

function PriceInputView:getTotalMoney()
    return checknumber(self.guildMoney) + checknumber(self.originalPrice)
end

function PriceInputView:setPrice(p)
    self.currentPrice = math.max(0, math.min(p, self:getTotalMoney()))
    self.lbPrice:setString(tostring(self.currentPrice))


    self:centerNodes({self.lbPrice, self.icnPrice})
end


function PriceInputView:centerNodes(nodes)
    local w1 = nodes[1]:getContentSize().width
    local w2 = nodes[2]:getContentSize().width
    local pw = nodes[1]:getParent():getContentSize().width

    local ow = (pw - (w1 + w2)) * 0.5

    nodes[1]:setPositionX(ow + w1 * 0.5)
    nodes[2]:setPositionX(ow + w1 + w2 * 0.5)
end

function PriceInputView:refresh(data)
    self._data = data
    if data then
        self:refreshPrice(data)
        self:refreshButton()
    end
end

return PriceInputView